PROGRAM   := kernel

SUBDIRS   := lib driver boot
INCDIR    := ./include

LDSCRIPT  = kernel.ld
LIBC 	  = ./driver/libdriver.a ./lib/libc.a 

LDFLAGS   :=  -nostdlib
LDFLAGS   += -T $(LDSCRIPT)

-include MK/Makefile.x86

define make_subdir
 @for subdir in $(SUBDIRS) ; do \
 ( cd $$subdir && make $1) \
 done;
endef

.PHONY : all deps objs clean cleanall rebuild floppy

all : deps $(PROGRAM)

deps: 
	gcc -MM -MD *.c

# Rules for producing the objects.
#---------------------------------------------------
objs : $(OBJS)

%.o : %.c
	$(CC) -c $(CFLAGS) -o$@ $<

%.o : %.asm
	$(NASM) -f elf -g -o$@  $<

subdirs:
	$(call make_subdir, all)

subdirs_clean:
	$(call make_subdir, clean)

# Rules for producing the executable.
#----------------------------------------------
$(PROGRAM) : subdirs $(OBJS) 
	$(LD) $(LDFLAGS) -o$@ $(OBJS) $(LIBC)
#	strip $@

floppy:
	$(MAKE) -C image floppy

-include $(DEPS)

rebuild: clean all

clean : subdirs_clean
	rm -f *.o *.d

cleanall: clean
	@$(RM) -f $(PROGRAM) *.o semantic.cache
